package nowcoder.offer;

import java.util.ArrayList;
import java.util.LinkedList;

/**
 * 把二叉树打印成多行
 *
 * @author briarbear
 * @blog https://briarbear.github.io
 * @date 2018-08-30 10:45
 */
public class PrintMore {

    ArrayList<ArrayList<Integer>> solution(TreeNode pRoot){

        ArrayList<ArrayList<Integer>> res = new ArrayList<>();
        if (pRoot == null) return res;
        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.offer(pRoot);
        while (!queue.isEmpty()) {
            LinkedList<TreeNode> nodes = new LinkedList<>();
            while (!queue.isEmpty()) {
                nodes.offer(queue.poll());
            }
            ArrayList<Integer> list = new ArrayList<>();
            while (!nodes.isEmpty()) {
                TreeNode node = nodes.poll();
                if (node.left != null) queue.offer(node.left);
                if (node.right != null) queue.offer(node.right);
                list.add(node.val);
            }
            res.add(list);
        }

        return res;
    }
}
